package com.opticon.vaadinR.ui.demo;

import com.opticon.vaadinR.interfaces.VaadinRApplication;
import com.vaadin.data.Property;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.VerticalLayout;
import org.apache.log4j.Logger;

import java.util.Observable;
import java.util.Observer;

/**
 * This class is a
 */
public abstract class AbstractInputPanel extends VerticalLayout implements Property.ValueChangeListener, Observer {
    private static Logger logger = Logger.getLogger(DensityInputPanel.class);

    private Property iSelectedProperty = null;
    public ProgressIndicator iExecutionProgress;

    public Button iExecuteButton;

    protected final VaadinRApplication iVaadinRApplication;
    protected HorizontalLayout iContent;


    public AbstractInputPanel(VaadinRApplication aVaadinRApplication) {
        iVaadinRApplication = aVaadinRApplication;

        setSpacing(true);

        addInputFields();

        iExecutionProgress = new ProgressIndicator();
        iExecutionProgress.setIndeterminate(true);
        iExecutionProgress.setPollingInterval(5000);
        iExecutionProgress.setEnabled(false);

        iExecuteButton = new Button("run");


        iContent = new HorizontalLayout();
        iContent.addComponent(iExecuteButton);
        iContent.addComponent(iExecutionProgress);

        addComponent(iContent);

        initExecutionListener();
    }

    protected abstract void addInputFields();

    protected abstract void initExecutionListener();



    /*
    * Shows a notification when a selection is made. The listener will com
    * called whenever the value of the field changes, i.e when the user
    * makes a new selection.
    */
    public void valueChange(Property.ValueChangeEvent event) {
        iSelectedProperty = event.getProperty();
        logger.debug("selected option " + iSelectedProperty);
    }


    public void update(Observable aObservable, Object o) {
        iExecutionProgress.setEnabled(false);
        iExecuteButton.setEnabled(true);
        requestRepaintAll();
    }
}
